using Microsoft.AspNetCore.Mvc;
using Infrastructure.Filters;
using ZR.Infrastructure;
using ZR.Service.BaseFrame.IService;
using ZR.Model.BaseFrame.Dto;
using ZR.Model.BaseFrame.Entity;
using ZR.Model;
using ZR.Admin.WebApi.MainModules;
using ZR.ServiceCore.Attributes;

namespace ZR.Admin.WebApi.Controllers.BaseFrame
{
    /// <summary>
    /// 用户
    /// </summary>
    public class UserControllerBase : ApiControllerBase
    {

        /// <summary>
        /// 用户接口
        /// </summary>
        protected readonly IUserService _UserService;

        protected readonly MapsterAdaptEntityCfg _mapsterAdaptEntityCfg;

        public UserControllerBase(IUserService userService, MapsterAdaptEntityCfg mapsterAdaptEntityCfg)
        {
            _UserService = userService;
            _mapsterAdaptEntityCfg = mapsterAdaptEntityCfg;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        [HttpPost("add")]
        [ActionPermissionFilter(Permission = "user:add")]
        [Log(Title = "用户", BusinessType = BusinessType.INSERT)]
        public virtual async Task AddUser([FromBody] UserFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<UserEntity>(_mapsterAdaptEntityCfg).ToCreate(HttpContext, EntityExtension.AutoFillField2);
            var isOk = await _UserService.AddUser(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "添加失败");
            }
        }

        /// <summary>
        /// 修改用户
        /// </summary>
        /// <returns></returns>
        [HttpPost("modify")]
        [ActionPermissionFilter(Permission = "user:edit")]
        [Log(Title = "用户", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ModifyUser([FromBody] UserFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<UserEntity>(_mapsterAdaptEntityCfg).ToUpdate(HttpContext);
            var isOk = await _UserService.ModifyUser(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "user:delete")]
        [Log(Title = "用户", BusinessType = BusinessType.DELETE)]
        public virtual async Task DeleteUser([FromBody] DeleteUserReq req)
        {
            var idArr = Tools.SplitAndConvert<long>(req.Ids);
            var isOk = await _UserService.DeleteUser(idArr, false);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "删除失败");
            }
        }

        /// <summary>
        /// 变更用户状态
        /// </summary>
        /// <returns></returns>
        [HttpPost("changeState")]
        [ActionPermissionFilter(Permission = "user:changestate")]
        [Log(Title = "用户", BusinessType = BusinessType.UPDATE)]
        public virtual async Task ChangeUserState([FromBody] ChangeUserStateReq req)
        {
            var isOk = await _UserService.ChangeUserState(req);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }






        /// <summary>
        /// 查询用户分页列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("pageList")]
        [ActionPermissionFilter(Permission = "user:list")]
        public virtual async Task<PagedInfo<UserListItemDto>> GetUserPageList([FromBody] GetUserPageListReq req)
        {
            var result = await _UserService.GetUserPageList(req);
            return result;
        }

        /// <summary>
        /// 查询用户详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        [ActionPermissionFilter(Permission = "user:detail")]
        public virtual async Task<UserDetailDto> GetUserDetail(long id)
        {
            var result = await _UserService.GetUserDetail(id);
            return result;
        }

    }
}